package com.project.demo.controller;

import com.project.demo.entity.UserParticipationAttendee;
import com.project.demo.service.UserParticipationAttendeeService;
import com.project.demo.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名参会人信息：(UserParticipationAttendee)表控制层
 */
@Slf4j
@RestController
@RequestMapping("/user_participation_attendee")
public class UserParticipationAttendeeController extends BaseController<UserParticipationAttendee, UserParticipationAttendeeService> {

    @Autowired
    public UserParticipationAttendeeController(UserParticipationAttendeeService service) {
        setService(service);
    }

    /**
     * 批量添加参会人信息
     */
    @PostMapping("/batch_add")
    public Map<String, Object> batchAdd(HttpServletRequest request) {
        try {
            Map<String, Object> body = service.readBody(request.getReader());
            
            Object userParticipationIdObj = body.get("user_participation_id");
            if (userParticipationIdObj == null) {
                return error(400, "缺少必要参数：user_participation_id");
            }
            
            Integer userParticipationId = Integer.parseInt(userParticipationIdObj.toString());
            
            Object attendeesObj = body.get("attendees");
            if (attendeesObj == null) {
                return error(400, "缺少必要参数：attendees");
            }
            
            List<Map<String, Object>> attendees;
            if (attendeesObj instanceof List) {
                @SuppressWarnings("unchecked")
                List<Object> attendeesList = (List<Object>) attendeesObj;
                attendees = new java.util.ArrayList<>();
                for (Object item : attendeesList) {
                    if (item instanceof Map) {
                        @SuppressWarnings("unchecked")
                        Map<String, Object> attendeeMap = (Map<String, Object>) item;
                        attendeeMap.put("user_participation_id", userParticipationId);
                        // 处理字段名映射
                        if (attendeeMap.containsKey("phoneCode")) {
                            attendeeMap.put("phone_code", attendeeMap.remove("phoneCode"));
                        }
                        if (attendeeMap.containsKey("affiliationEn")) {
                            attendeeMap.put("affiliation_en", attendeeMap.remove("affiliationEn"));
                        }
                        if (attendeeMap.containsKey("speechTitle")) {
                            attendeeMap.put("speech_title", attendeeMap.remove("speechTitle"));
                        }
                        if (attendeeMap.containsKey("idCard")) {
                            attendeeMap.put("id_card", attendeeMap.remove("idCard"));
                        }
                        if (attendeeMap.containsKey("participationType")) {
                            attendeeMap.put("participation_type", attendeeMap.remove("participationType"));
                        }
                        // 处理 accommodation
                        Object accObj = attendeeMap.get("accommodation");
                        if (accObj instanceof Boolean) {
                            attendeeMap.put("accommodation", ((Boolean) accObj) ? 1 : 0);
                        } else if (accObj instanceof String) {
                            String accStr = (String) accObj;
                            attendeeMap.put("accommodation", (accStr.equals("true") || accStr.equals("1") || accStr.equals("是")) ? 1 : 0);
                        }
                        attendees.add(attendeeMap);
                    }
                }
            } else {
                return error(400, "attendees 参数格式错误，应为数组");
            }
            
            int successCount = 0;
            for (Map<String, Object> attendee : attendees) {
                try {
                    service.insert(attendee);
                    successCount++;
                } catch (Exception e) {
                    log.error("插入参会人信息失败: {}", attendee, e);
                }
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("success_count", successCount);
            result.put("total_count", attendees.size());
            return success(result);
            
        } catch (Exception e) {
            log.error("批量添加参会人信息失败", e);
            return error(500, "批量添加参会人信息失败: " + e.getMessage());
        }
    }
}

